﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using ZO.SmartCore.Helpers;
using ZO.SmartCore.Xml;

#endregion

namespace ZO.SmartCore.Protocols.XMPP.Protocol.Common
{
    /// <summary>
    /// Represents an error within a Stream Error.
    /// </summary>
    public class StreamError : Element
    {

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="StreamError"/> class.
        /// </summary>
        public StreamError()
            : base("error", "", UriList.XmppStream, "stream", "")
        {
        } // Error
        #endregion

        #region Destructor

        #endregion

        #region Fields

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the Error condition.
        /// </summary>
        /// <value>The condition.</value>
        public StreamErrorCondition Condition
        {
            get
            {
                if (this.HasChildElement("bad-format"))
                {
                    return StreamErrorCondition.BadFormat;
                } // if
                if (this.HasChildElement("bad-namespace-prefix"))
                {
                    return StreamErrorCondition.BadNamespacePrefix;
                } // if
                if (this.HasChildElement("conflict"))
                {
                    return StreamErrorCondition.Conflict;
                } // if
                if (this.HasChildElement("connection-timeout"))
                {
                    return StreamErrorCondition.ConnectionTimeout;
                } // if
                if (this.HasChildElement("host-gone"))
                {
                    return StreamErrorCondition.HostGone;
                } // if
                if (this.HasChildElement("host-unknown"))
                {
                    return StreamErrorCondition.HostUnknown;
                } // if
                if (this.HasChildElement("improper-addressing"))
                {
                    return StreamErrorCondition.ImproperAddressing;
                } // if
                if (this.HasChildElement("internal-server-error"))
                {
                    return StreamErrorCondition.InternalServerError;
                } // if
                if (this.HasChildElement("invalid-from"))
                {
                    return StreamErrorCondition.InvalidFrom;
                } // if
                if (this.HasChildElement("invalid-id"))
                {
                    return StreamErrorCondition.InvalidID;
                } // if
                if (this.HasChildElement("invalid-namespace"))
                {
                    return StreamErrorCondition.InvalidNamespace;
                } // if
                if (this.HasChildElement("invalid-xml"))
                {
                    return StreamErrorCondition.InvalidXml;
                } // if
                if (this.HasChildElement("not-authorized"))
                {
                    return StreamErrorCondition.NotAuthorized;
                } // if
                if (this.HasChildElement("policy-violation"))
                {
                    return StreamErrorCondition.PolicyViolation;
                } // if
                if (this.HasChildElement("remote-connection-failed"))
                {
                    return StreamErrorCondition.RemoteConnectionFailed;
                } // if
                if (this.HasChildElement("resource-constraint"))
                {
                    return StreamErrorCondition.ResourceConstraint;
                } // if
                if (this.HasChildElement("restricted-xml"))
                {
                    return StreamErrorCondition.RestrictedXml;
                } // if
                if (this.HasChildElement("see-other-host"))
                {
                    return StreamErrorCondition.SeeOtherHost;
                } // if
                if (this.HasChildElement("system-shutdown"))
                {
                    return StreamErrorCondition.SystemShutdown;
                } // if
                if (!this.HasChildElement("undefined-condition"))
                {
                    return StreamErrorCondition.UndefinedCondition;
                } // if
                if (!this.HasChildElement("unsupported-encoding"))
                {
                    return StreamErrorCondition.UnsupportedEncoding;
                } // if
                if (!this.HasChildElement("unsupported-stanza-type"))
                {
                    return StreamErrorCondition.UnsupportedStanzaType;
                } // if
                if (!this.HasChildElement("unsupported-version"))
                {
                    return StreamErrorCondition.UnsupportedVersion;
                } // if
                if (!this.HasChildElement("xml-not-well-formed"))
                {
                    return StreamErrorCondition.XmlNotWellFormed;
                } // if
                return StreamErrorCondition.None;

            } // get
            set
            {
                if (value != StreamErrorCondition.None)
                {
                    string stringValue = ReflectionHelper.GetDescription(value);
                    this[stringValue] = new Element(stringValue, "", UriList.StreamErrors);
                }

            } // set
        } // Condition

        /// <summary>
        /// Gets text description of the error.
        /// </summary>
        /// <value>The message.</value>
        public string Message
        {
            get
            {
                if (this.HasChildElement("text"))
                {
                    Element text = this["text"];

                    return text.Value;
                }


                switch (this.Condition)
                {
                    case StreamErrorCondition.BadFormat:
                        return "the entity has sent XML that cannot be processed; this error MAY be used instead of the more specific XML-related errors, such as <bad-namespace-prefix/>, <invalid-xml/>, <restricted-xml/>, <unsupported-encoding/>, and <xml-not-well-formed/>, although the more specific errors are preferred.";
                    
                    case StreamErrorCondition.BadNamespacePrefix:
                        return "the entity has sent a namespace prefix that is unsupported, or has sent no namespace prefix on an element that requires such a prefix.";

                    case StreamErrorCondition.Conflict:
                        return "the server is closing the active stream for this entity because a new stream has been initiated that conflicts with the existing stream.";

                    case StreamErrorCondition.ConnectionTimeout:
                        return "the entity has not generated any traffic over the stream for some period of time (configurable according to a local service policy).";

                    case StreamErrorCondition.HostGone:
                        return "the value of the 'to' attribute provided by the initiating entity in the stream header corresponds to a hostname that is no longer hosted by the server.";

                    case StreamErrorCondition.HostUnknown:
                        return "the value of the 'to' attribute provided by the initiating entity in the stream header does not correspond to a hostname that is hosted by the server.";

                    case StreamErrorCondition.ImproperAddressing:
                        return "a stanza sent between two servers lacks a 'to' or 'from' attribute (or the attribute has no value).";

                    case StreamErrorCondition.InternalServerError:
                        return "the server has experienced a misconfiguration or an otherwise-undefined internal error that prevents it from servicing the stream.";

                    case StreamErrorCondition.InvalidFrom:
                        return "the JID or hostname provided in a 'from' address does not match an authorized JID or validated domain negotiated between servers via SASL or dialback, or between a client and a server via authentication and resource binding.";

                    case StreamErrorCondition.InvalidID:
                        return "the stream ID or dialback ID is invalid or does not match an ID previously provided.";

                    case StreamErrorCondition.InvalidNamespace:
                        return "the streams namespace name is something other than \"http://etherx.jabber.org/streams\" or the dialback namespace name is something other than \"jabber:server:dialback\" ";

                    case StreamErrorCondition.InvalidXml:
                        return "the entity has sent invalid XML over the stream to a server that performs validation.";

                    case StreamErrorCondition.NotAuthorized:
                        return "the entity has attempted to send data before the stream has been authenticated, or otherwise is not authorized to perform an action related to stream negotiation; the receiving entity MUST NOT process the offending stanza before sending the stream error.";

                    case StreamErrorCondition.PolicyViolation:
                        return "the entity has violated some local service policy; the server MAY choose to specify the policy in the <text/> element or an application-specific condition element.";

                    case StreamErrorCondition.RemoteConnectionFailed:
                        return "the server is unable to properly connect to a remote entity that is required for authentication or authorization.";

                    case StreamErrorCondition.ResourceConstraint:
                        return "the server lacks the system resources necessary to service the stream.";

                    case StreamErrorCondition.RestrictedXml:
                        return "the entity has attempted to send restricted XML features such as a comment, processing instruction, DTD, entity reference, or unescaped character";

                    case StreamErrorCondition.SeeOtherHost:
                        return "the server will not provide service to the initiating entity but is redirecting traffic to another host; the server SHOULD specify the alternate hostname or IP address (which MUST be a valid domain identifier) as the XML character data of the <see-other-host/> element.";

                    case StreamErrorCondition.SystemShutdown:
                        return "the server is being shut down and all active streams are being closed.";

                    case StreamErrorCondition.UndefinedCondition:
                        return "the error condition is not one of those defined by the other conditions in this list; this error condition SHOULD be used only in conjunction with an application-specific condition.";

                    case StreamErrorCondition.UnsupportedEncoding:
                        return "the initiating entity has encoded the stream in an encoding that is not supported by the server.";

                    case StreamErrorCondition.UnsupportedStanzaType:
                        return "the initiating entity has sent a first-level child of the stream that is not supported by the server.";

                    case StreamErrorCondition.UnsupportedVersion:
                        return "the value of the 'version' attribute provided by the initiating entity in the stream header specifies a version of XMPP that is not supported by the server; the server MAY specify the version(s) it supports in the <text/> element.";

                    case StreamErrorCondition.XmlNotWellFormed:
                        return "the initiating entity has sent XML that is not well-formed.";

                    default:
                        return "";
                }

            }
        }
        #endregion

        #region Methods

        #endregion
    }
}
